package com.dmlll.config;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class ToExcel<T> {

    public void excel1(String filename, String[] heards, HttpServletResponse response, List<List<String>> list){
        Workbook workbook=new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("oneSheet");
        Row row = sheet.createRow(0);
        for (int i=0;i<heards.length;i++){
            row.createCell(i).setCellValue(heards[i]);
        }
        for(int i=0;i<list.size();i++){
            List<String> list1=new ArrayList<>();
            list1=list.get(i);
            Row row1=sheet.createRow(i+1);
            for (int j=0;j<list1.size();j++){
                row1.createCell(j).setCellValue(list1.get(j));
            }
        }
        try {
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename,"UTF-8")+".xlsx");//附件形式下载
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            workbook.write(response.getOutputStream());  //写到客户端
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public void excel(String filename, String[] heards, HttpServletResponse response, List<List<String>> list)  {
        //声明一个workbook工作薄
        Workbook workbook=new XSSFWorkbook();
        //声明sheet工作表
        Sheet sheet=workbook.createSheet("one");
        //创建行
        Row row=sheet.createRow(0);
        for (int i = 0; i < heards.length; i++) {
            //创建头信息
//            Cell cell=null;
//            cell=row.createCell(i);
//            cell.setCellValue(heards[i]);
            row.createCell(i).setCellValue(heards[i]);
        }
        for (int i = 0; i < list.size(); i++) {
            List<String> stringList=list.get(i);
            Row row1=sheet.createRow(i+1);
            for (int i1 = 0; i1 < stringList.size(); i1++) {
                Cell cell=row1.createCell(i1);
                String value=stringList.get(i1);
                cell.setCellValue(value);
            }
        }
        try {
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename,"UTF-8")+".xlsx");//附件形式下载
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            workbook.write(response.getOutputStream());  //写到客户端
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
